<!DOCTYPE html>
<html lang="zh-CN">
{% load static %}
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>找回密码</title>
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}">
	<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrapValidator.min.css' %}">
    <style>
        body{
            width: 100%;
            height: calc(100vh - 100px);
            background: url("{% static 'img/bg-01.jpg' %}")no-repeat;
            background-size: cover;
        }
        #main{
            position: fixed;
            width: 395px;
            background: #fff;
            border: 1px solid #ccc;
            top: 50%;
            left: 50%;
            margin-top: -240px;
            margin-left: -197.5px;
            box-shadow: 0px 0px 5px #333333;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;
        }
        .has-feedback .form-control {
            padding-right: 12px;
        }
        .input-group{
            width: 100%;
        }
.login-top {
    font-size: 24px;
    margin-top: 30px;
    padding-left: 40px;
    box-sizing: border-box;
    color: #333333;
    margin-bottom: 20px;
}
.login-center {
    width: 100%;
    box-sizing: border-box;
    padding: 0 40px;
    margin-bottom: 20px!important;
}
.login-button {
    cursor: pointer;
    width: 313px;
    text-align: center;
    height: 40px;
    background-color: dodgerblue;
    border-radius: 5px;
    margin: 0 40px;
    color: white;
}
        .login-button:hover{
            color: white;
        }
        a{
            text-decoration: none;
            color: #333;
        }
        a:hover{
            text-decoration: none;
            color: #333;
        }
        .error{
            font-size: 14px;
            color: red;
           position: relative;
            left: 40px;
            top: -10px;
        }
        #main>p{
            margin: 20px 40px;
        }
        #main>p>.fl{
            float: left;
        }
        #main>p>.fr{
            float: right;
        }
    </style>
</head>

<body>
<div id="main">
     <form class="form-inline" id="defaultForm">
         {% csrf_token %}
         <div class="login-top">
				找回密码
			</div>

         <div class="form-group login-center">

            <div class="input-group">
              <div class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></div>
              <input type="email" class="form-control verification" name="email" placeholder="请输入邮箱"required >
            </div>
         </div>
         <input type="hidden" id="hidden">
          <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"style="width: 48px"><i class="glyphicon glyphicon-phone"></i></div>
          <input type="text" class="form-control verification"required name="captcha" placeholder="请输入验证码"style="width: 50%">
            <button type="button" class="btn btn-primary" onclick="Verification()"id="zphone">获取验证码</button>

        </div>
      </div>
       <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></div>
          <input type="password" class="form-control verification" name="password" minlength="6"required placeholder="请输入密码">
        </div>
      </div>
         <div class="form-group login-center">

            <div class="input-group ">
              <div class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></div>
              <input type="password" class="form-control verification" name="password1" minlength="6"required placeholder="请确认密码">
            </div>
      </div>
        <p class="errors"></p>

  <button type="button" class="btn login-button " id="submit">提交</button>
</form>
    <p class="clearfix">
        <a class="fl" href="/login">已有账号?立即登录</a>
        <a class="fr" href="{% url 'home' %}">首页</a>
    </p>
</div>

	<script src="{% static 'js/jquery-3.3.1.min.js' %}"></script>
	<script src="{% static 'js/bootstrap.min.js' %}"></script>
	<script src="{% static 'js/bootstrapValidator.min.js' %}"></script>
	<script src="{% static 'js/language/zh_CN.js' %}"></script>
    <script src="{% static 'js/jquery.cookie.min.js' %}"></script>
    <script src="{% static 'js/promise.min.js' %}"></script>
    <script src="{% static 'js/sweetalert.min.js' %}"></script>
<script>
    var iTime = 60;
 sTime = ''
    function RemainTime(){
        if (iTime == 0) {
            document.getElementById('zphone').disabled = false;
            sTime="获取验证码";
            iTime = 60;
            document.getElementById('zphone').innerHTML = sTime;
            return;
        }else{
            document.getElementById('zphone').disabled = true;
            sTime="重新发送(" + iTime + ")";
            iTime--;
        }
        setTimeout(function() { RemainTime() },1000)
        document.getElementById('zphone').innerHTML = sTime;
    }
    //更换邮箱获取验证码
    function Verification() {
        var email = $("input[name='email']").val()
        $.ajax({
            url:'/person/retrieveEmail/',
            type:'post',
              headers:{'X-CSRFToken':$.cookie('csrftoken')},
            data:{email:email},
            success:function (data) {

                 if(data.status==200){
                     RemainTime()
                     swal({
                          text: data.message,
                          icon: "success",
                        })
                }else {
                     swal({
                      text: data.message,
                      icon: "error",
                    })
                }

            }
        })

    }
       $('#defaultForm')
        .bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                email: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        emailAddress: {
                            message: '请输入正确邮箱'
                        }
                    }
                },
                password: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                          identical: {//相同
                         field: 'password', //需要进行比较的input name值
                         message: '两次密码不一致'
                     },
                          stringLength: {  //长度限制
                          min: 6,
                          message: '密码不能小于6位'
                        },
                    }
                },
                password1: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                          identical: {//相同
                         field: 'password', //需要进行比较的input name值
                         message: '两次密码不一致'
                     },
                          stringLength: {  //长度限制
                          min: 6,
                          message: '密码不能小于6位'
                        },
                    }
                },

            }
        })

    $('#submit').on('click',function () {

        $("form").bootstrapValidator('validate');//提交验证
                if ($("form").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
                    var obj={}
                    $(".verification").each(function (i,item) {
                        obj[$(item).attr('name')]=$(item).val()
                    })
                    $.ajax({
                        url:'/person/retrieve/',
                        type:'POST',
                        data:obj,
                        headers:{'X-CSRFToken':$.cookie('csrftoken')},
                        success:function (data) {
                            $('.errors').html('');
                            if(data.status==200){
                                  swal({
                                  text: data.message,
                                  icon: "success",
                                }).then(function () {
                           window.location.href='/login'
                       })
                            }else{
                                swal({
                                  text: data.message,
                                  icon: "error",
                                })
                            }

                        }

                    })

                }

        console.log(obj)
    })
</script>
</body>

</html>